﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLULearningNetworkModel
{
    public partial class FeedForFriend
    {
        /// <summary>
        /// Them feedForFriend
        /// </summary>
        /// <param name="_FeedForFriend"></param>
        /// <returns>0 : them khong thanh cong, 1 : thanh cong, 2 : da co roi, 101 : catch</returns>
        public static int Add(FeedForFriend _FeedForFriend)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    FeedForFriend feedForFriend = db.FeedForFriends.SingleOrDefault(fff => fff.AccountFriendID_u == _FeedForFriend.AccountFriendID_u && fff.FeedID_u == _FeedForFriend.FeedID_u);
                    if (feedForFriend != null)
                    {
                        return 2;
                    }
                    db.FeedForFriends.AddObject(_FeedForFriend);
                    if (db.SaveChanges() > 0)
                    {
                        return 1;
                    }
                    return 0;
                }
            }
            catch
            {
                return 101;
            }
        }

        public static int AddFeedForFriend(Guid feedID, Guid activeAccount)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    List<Friend> lstFriend = Friend.SelectAllFriendOfAnAccount(activeAccount);
                    FeedForFriend feedForFriend;
                    if (lstFriend != null && lstFriend.Count > 0)
                    {
                        foreach (Friend f in lstFriend)
                        {
                            feedForFriend = new FeedForFriend();
                            feedForFriend.FeedForFriendID_u = Guid.NewGuid();
                            feedForFriend.FeedID_u = feedID;
                            feedForFriend.AccountFriendID_u = f.AccountID_u == activeAccount ? f.FriendAccountID_u : f.AccountID_u;
                            db.FeedForFriends.AddObject(feedForFriend);
                        }
                        if (db.SaveChanges() > 0)
                        {
                            return 1;
                        }
                        else
                        {
                            return 2;
                        }
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            catch
            {
                return 101;
            }
        }

        public static int AddFeedForCourseMember(Feed feed, Guid courseID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    List<Guid> lstMemberAccountID = (from enrollment in db.Enrollments
                                                     where enrollment.CourseID_u == courseID
                                                     && enrollment.Status_i == 1
                                                     select enrollment.AccountID_u).ToList();
                    FeedForFriend feedForFriend;
                    if (lstMemberAccountID != null && lstMemberAccountID.Count != 0)
                    {
                        foreach (Guid memberAccountID in lstMemberAccountID)
                        {
                            feedForFriend = new FeedForFriend();
                            feedForFriend.FeedForFriendID_u = Guid.NewGuid();
                            feedForFriend.FeedID_u = feed.FeedID_u;
                            feedForFriend.AccountFriendID_u = memberAccountID;
                            FeedForFriend.Add(feedForFriend);
                        }
                        if (db.SaveChanges() > 0)
                        {
                            return 1;
                        }
                    }
                    return 0;

                }
            }
            catch
            {
                return 0;
            }
        }

        public static List<FeedForFriend> SelectFeedForFriendOfAccount(Guid accountID, int numberOfSkip, int numberOfTake)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    var query = db.FeedForFriends.Include("Feed")
                        .Include("Feed.Account")
                        .Include("Feed.Account1")
                        .Include("Feed.Course")
                        .Include("Feed.FeedComments")
                        .Include("Feed.FeedComments.Account")
                        .Where(fff => fff.AccountFriendID_u == accountID)
                        .OrderByDescending(fff => fff.Feed.LastUpdate_dt)
                        .Skip(numberOfSkip)
                        .Take(numberOfTake);
                    return query.ToList();
                }
            }
            catch
            {
                return null;
            }
        }

        public static List<FeedForFriend> SelectLatestFeedForFriendOfAccount(Guid accountID, DateTime time)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    var query = db.FeedForFriends.Include("Feed")
                        .Include("Feed.Account")
                        .Include("Feed.Account1")
                        .Include("Feed.Course")
                        .Include("Feed.FeedComments")
                        .Include("Feed.FeedComments.Account")
                        .Where(fff => fff.AccountFriendID_u == accountID && fff.Feed.LastUpdate_dt.CompareTo(time)>0)
                        .OrderByDescending(fff => fff.Feed.LastUpdate_dt);
                    return query.ToList();
                }
            }
            catch
            {
                return null;
            }
        }

        public static int Delete(Guid feedID, Guid account)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    FeedForFriend feedForFriend = db.FeedForFriends.FirstOrDefault(v => v.FeedID_u == feedID && v.AccountFriendID_u == account);
                    if (feedForFriend != null)
                    {
                        db.FeedForFriends.DeleteObject(feedForFriend);
                        if (db.SaveChanges() > 0)
                        {
                            return 1;
                        }
                    }
                    return 0;
                }
            }
            catch
            {
                return 101;
            }
        }

    }
}
